package com.example.exp_4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText account,password;
        TextView register;
        Button btlogin;
        register = (TextView) findViewById(R.id.register);
        account = (EditText) findViewById(R.id.account);
        password = (EditText) findViewById(R.id.password);
        btlogin = (Button) findViewById(R.id.login);

        SharedPreferences sharedPreferences = getSharedPreferences("person",MODE_PRIVATE);
        account.setText(sharedPreferences.getString("number",""));
        password.setText(sharedPreferences.getString("password",""));
        //account.setText(sharedPreferences.getString(""));
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Register.class);
                startActivity(intent);
            }
        });
        btlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (account.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"请输入账号",Toast.LENGTH_SHORT).show();
                } else if (password.getText().toString().equals("")) {
                    Toast.makeText(getApplicationContext(),"请输入密码",Toast.LENGTH_SHORT).show();
                } else if (!account.getText().toString().equals(sharedPreferences.getString("number","")) || !password.getText().toString().equals(sharedPreferences.getString("password",""))) {
                    Toast.makeText(getApplicationContext(),"账号或密码错误",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),"登陆成功",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this,Menu.class);
                    startActivity(intent);
                }
            }
        });
    }
}